<#include "/layout/header.ftl" />
<#import "/include/ui.ftl" as blue />

<script>

$(function()
{
	
});

function save_click()
{
	var obj = $.getObject("#div_form");
	var menuIds = new Array();
	var authIds = new Array();
	
	$.each($("#div_form :checkbox:checked"), function(i, obj)
	{
		var $obj = $(obj);
		var type = $obj.attr("name");
		if (type == "menuId")
		{
			menuIds.push($obj.val());
		}
		else if (type == "authId")
		{
			authIds.push($obj.val());
		}
	});
	
	/* if (menuIds.length == 0)
	{
		$.errorAlert("至少选择一个菜单");
		return;
	}
	
	if (authIds.length == 0)
	{
		$.errorAlert("至少选择一个菜单权限");
		return;
	} */
	
	obj.menuIds = menuIds.join(",");
	obj.authIds = authIds.join(",");
	$.simpleAjax("/base/auth/role_menu_save", obj, function(o)
	{
		$.successAlert("保存成功", "save_success()", 2000);
	});

}
function save_success()
{
	window.location = "${path}/base/auth/role_list";
}

function menu_select(id, type)
{
	if (type == 0) // 全选
	{
		$("div[parent_id="+id+"] :checkbox").prop("checked", true);
		$("#chk_menu_"+id).prop("checked", true);
	}
	else if (type == 1) // 全不选
	{
		$("div[parent_id="+id+"] :checkbox").prop("checked", false);
		$("#chk_menu_"+id).prop("checked", false);
	}
}

function sub_menu_select(id, type)
{
	if (type == 0) // 全选
	{
		$("#div_auth_"+id+" :checkbox").prop("checked", true);
		$("#chk_menu_"+id).prop("checked", true);
	}
	else if (type == 1) // 全不选
	{
		$("#div_auth_"+id+" :checkbox").prop("checked", false);
		$("#chk_menu_"+id).prop("checked", false);
	}
}



</script>

<div class="am-form am-form-horizontal am-cf am-margin" id="div_form">
<input type="hidden" name="roleId" value="${model.id!}"/>

	<fieldset>
		<legend>修改角色权限－${model.name!}－${app.name!}</legend>
		
		<#assign list=menuList["0"]! />
		<#if list??>
		<#list list as vo>
		<div class="am-checkbox input_big">
			<label>
				<input type="checkbox" id="chk_menu_${vo.id}" name="menuId" value="${vo.id}" <#if vo.selected==0>checked</#if> /> ${vo.name!}
			</label>
			<a href="javascript:void(0)" class="am-margin-left-xl" onclick="menu_select('${vo.id}', 0)">（全选</a>
			<a href="javascript:void(0)" class="am-margin-left" onclick="menu_select('${vo.id}', 1)">全不选）</a>
		</div>

		<@tree map=menuList key=vo.id?string count=1 />
		</#list>
		</#if>

		<div class="am-u-md-12 am-text-center div_btn">
			<a href="javascript:void(0)" onclick="save_click()" class="am-btn am-btn-primary am-radius">保存</a>
			<a href="javascript:void(0)" onclick="save_success()" class="am-btn am-btn-default am-radius am-margin-left">返回</a>
		</div>
	</fieldset>
</div>

<#macro tree map key count=0>
	<#assign subList=map[key]! />
	<#if subList??>
	<#list subList as sub>
	
		<div class="am-checkbox input_normal am-margin-left" parent_id="${key}">
			<label>
				<input type="checkbox" id="chk_menu_${sub.id}" name="menuId" value="${sub.id}" <#if sub.selected==0>checked</#if> /> ${sub.name!}
			</label>
			<a href="javascript:void(0)" class="am-margin-left-xl" onclick="sub_menu_select('${sub.id}', 0)">（全选</a>
			<a href="javascript:void(0)" class="am-margin-left" onclick="sub_menu_select('${sub.id}', 1)">全不选）</a>
		</div>
		
		<#assign authList=sub.authList! />
		<#if authList??>
		<div class="am-form-group input_normal am-margin-left" parent_id="${key}" id="div_auth_${sub.id}">
		<#list authList as auth>
			<label class="am-checkbox-inline">
				<input type="checkbox" name="authId" value="${auth.id}" <#if auth.selected==0>checked</#if>> ${auth.name}
			</label>
		</#list>
		</div>
		</#if>
		
		<@tree map=map key=sub.id?string count=count+1 />
		
	</#list>
	</#if>
</#macro>

<#include "/layout/footer.ftl" />